/*
 * @Descripttion: react相关工具类
 * @version: 
 * @Author: 黄杰
 * @Date: 2024-09-03 15:05:13
 * @LastEditors: 黄杰
 * @LastEditTime: 2024-09-03 15:17:26
 */
 import {useState,useEffect,useCallback} from 'react'
 
 /**
  * 生成状态更新后异步执行的函数方法
  * @param {function} callback 源函数
  * @returns {function} 状态更新后才执行的方法
  */
 export const useSyncCallback = (callback: Function) => {
    const [proxyState, setProxyState] = useState<{ current: boolean; params: any[] }>({ current: false, params: [] })
    const Func = useCallback(
      (...args: any[]) => {
    
        setProxyState({ current: true, params: args })
      },
      [proxyState]
    )

    useEffect(() => {
      if (proxyState.current === true) setProxyState({ current: false, params: proxyState.params })
    }, [proxyState])

    useEffect(() => {
      proxyState.current && callback(...proxyState.params)
    })
    return Func
  }